package com.wjw.tool;

import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.wjw.domain.PhonePackage;
import com.wjw.domain.query.PhonePackageQuery;
import com.wjw.service.PhonePackageService;
import org.springframework.ai.tool.annotation.Tool;
import org.springframework.ai.tool.annotation.ToolParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Component;

import java.util.List;

@Component
public class MyTool {
    @Autowired
    private PhonePackageService phonePackageService;
    /**
     * 按条件查询套餐
     * 需要参数： 费用预算，流量需求，通话需求，附加需求
     * @return
     */
    @Tool(description = "按条件查询套餐")
    public List<PhonePackage> queryPhonePackageByCondition(@ToolParam(description = "查询条件") PhonePackageQuery query){
        //select * from phone_package where monthly_rent between ? and ? and traffic >= ? and call_duration >= ? and preferential_active like %%
        QueryWrapper<PhonePackage> queryWrapper = new QueryWrapper<>();
        if (StrUtil.isNotBlank(query.getMoneyRange())){
            switch (query.getMoneyRange()){
                case "50元以下":
                    queryWrapper.le("monthly_rent",50);
                    break;
                case "50-100元":
                    queryWrapper.between("monthly_rent",50,100);
                    break;
                case "100元以上":
                    queryWrapper.gt("monthly_rent",100);
                    break;
                default:
                    break;
            }
        }
        if (StrUtil.isNotBlank(query.getTraffic())){
            switch (query.getTraffic()){
                case "轻度文字":
                    //推荐全部
                    break;
                case "视频刷剧":
                    queryWrapper.like("traffic","30GB")
                            .or()
                            .like("traffic","50GB")
                            .or()
                            .like("traffic","不限量");
                    break;
                case "直播":
                    queryWrapper.gt("traffic","不限量");
                    break;
                default:
                    break;
            }
        }
        if (StrUtil.isNotBlank(query.getCallNeed())){
            if (query.getCallNeed().contains("国际")){
                queryWrapper.like("additional_service","国际");
            }else{
                String callNeed = query.getCallNeed();
                // 校验格式：必须以"国内"开头、以"分钟"结尾
                if (callNeed == null || !callNeed.startsWith("国内") || !callNeed.endsWith("分钟")) {
                    return null;
                }
                // 移除前缀"国内"和后缀"分钟"，保留中间数字部分
                String numberStr = callNeed.substring(2, callNeed.length() - 2).trim();
                int num = Integer.parseInt(numberStr);
                queryWrapper.gt("call_duration",num);
            }
        }
        if (StrUtil.isNotBlank(query.getAddition())){
            if ("需要宽带".equals(query.getAddition())){
                queryWrapper.like("additional_service","宽带");
            }else {
                queryWrapper.notLike("additional_service","宽带");
            }
        }
        return phonePackageService.list(queryWrapper);
    }
}
